/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.SEC;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.HttpConstraint;
import javax.servlet.annotation.ServletSecurity;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import util.Constant.ConstantjQueryDialogState;
import util.jQueryForm.Dialog.jQueryFormDialog;

@WebServlet(name = "SEC_LoginController",
loadOnStartup = 1,
urlPatterns = {
    "/login/login_panel",
    "/SEC/Login",
    "/SEC/Login/doLogin",
    "/SEC/Login/doLogout"})
public class SEC_LoginController extends HttpServlet {

    // <editor-fold defaultstate="collapsed" desc="Init Param">
    private String sampleServletContext;

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {

        super.init(servletConfig);

        // initialize servlet with configuration information
        sampleServletContext = servletConfig.getServletContext().getInitParameter("sampleServletContext");
    }
    // </editor-fold>

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // <editor-fold defaultstate="collapsed" desc="Default Page Param">
        //Request
        request.setCharacterEncoding("UTF-8");  // ensures that user input is interpreted as // 8-bit Unicode (e.g., for Czech characters)
        String userPath = request.getServletPath();
        HttpSession session = request.getSession();
        //Response
        response.setContentType("text/html;charset=utf-8"); //text/json
        Boolean isDispatcher = true; //กำหนดว่า ต้องการให้เปลี่ยนหน้าหรือไม่ ? ต้องการ : ไม่ต้องการ
        // </editor-fold>

        // <editor-fold defaultstate="collapsed" desc="Control Page Redirect & Reset Requset-Attribute">
        if (userPath.equals("/SEC/Login")) {
            // <editor-fold defaultstate="collapsed" desc="URL Param">
            
            // </editor-fold>

            // <editor-fold defaultstate="collapsed" desc="Attribute Param">

            // </editor-fold>

            // <editor-fold defaultstate="collapsed" desc="Change Page Param">
            userPath = "/SEC/Login";
            isDispatcher = true;
            // </editor-fold>

        }
        // </editor-fold>

        // <editor-fold defaultstate="collapsed" desc="Change Page">
        // use RequestDispatcher to forward request internally
        String url = "/WEB-INF/view" + userPath + ".jsp";
        try {
            if (isDispatcher) {
                request.getRequestDispatcher(url).forward(request, response);
            }
        } catch (Exception ex) {
            //@TODO : Error Code Here
            ex.printStackTrace();
        }
        // </editor-fold>
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // <editor-fold defaultstate="collapsed" desc="Default Page Param">
        //Request
        request.setCharacterEncoding("UTF-8");  // ensures that user input is interpreted as // 8-bit Unicode (e.g., for Czech characters)
        String userPath = request.getServletPath();
        HttpSession session = request.getSession();
        //Response
        response.setContentType("text/json;charset=utf-8");
        PrintWriter out;
        String strJson = "{}";
        Boolean isDispatcher = false; //กำหนดว่า ต้องการให้เปลี่ยนหน้าหรือไม่ ? ต้องการ : ไม่ต้องการ
        // </editor-fold>

        // <editor-fold defaultstate="collapsed" desc="Request Function">
        if (userPath.equals("/SEC/Login/doLogin")) { //btnLogin Click
            // <editor-fold defaultstate="collapsed" desc="URL Param">
            String strUserName = (request.getParameter("userlog") != null) ? request.getParameter("userlog") : "";
            String strPassword = (request.getParameter("pwd") != null) ? request.getParameter("pwd") : "";
            String btnLogin = (request.getParameter("btnLogin") != null) ? request.getParameter("btnLogin") : "";
            // </editor-fold>

            // <editor-fold defaultstate="collapsed" desc="Attribute Param">

            // </editor-fold>

            // <editor-fold defaultstate="collapsed" desc="Param">
            
            // </editor-fold>

            // <editor-fold defaultstate="collapsed" desc="Coding">
            session.setAttribute("isLogin", "1");
            
            // </editor-fold>

            // <editor-fold defaultstate="collapsed" desc="Change Page Param">
            response.setContentType("text/json;charset=utf-8");
            userPath = "/SEC/Login";
            isDispatcher = false;
            // </editor-fold>
        } else if (userPath.equals("/SEC/Login/doLogout")) {
            // <editor-fold defaultstate="collapsed" desc="URL Param">

            // </editor-fold>

            // <editor-fold defaultstate="collapsed" desc="Attribute Param">

            // </editor-fold>

            // <editor-fold defaultstate="collapsed" desc="Param">

            // </editor-fold>

            // <editor-fold defaultstate="collapsed" desc="Coding">
            session.setAttribute("isLogin", null);
            session.invalidate();
            // </editor-fold>

            // <editor-fold defaultstate="collapsed" desc="Change Page Param">
            response.setContentType("text/json;charset=utf-8");
            userPath = "/SEC/Login";
            isDispatcher = false;
            // </editor-fold>
        }
        // </editor-fold>

        // <editor-fold defaultstate="collapsed" desc="Change Page">
        // use RequestDispatcher to forward request internally
        String url = "/WEB-INF/view" + userPath + ".jsp";
        try {
            if (isDispatcher) {
                request.getRequestDispatcher(url).forward(request, response);
            }
        } catch (Exception ex) {
            //@TODO : Error Code Here
            ex.printStackTrace();
        }
        // </editor-fold>
    }
}
